/*
* $Id: QBeePointView.cpp 122 2010-07-22 17:05:50Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeePointView.h"
#include "Database/Operation/QBInfoPointOperation.h"
#include "Database/Tables/PointsTable.h"
#include "Event/EventId.h"

CQBeePointView::CQBeePointView(CState* pState)
: CQBeeView(pState){
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->SubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	Init();
}
CQBeePointView::~CQBeePointView(){
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);
	IApplication::GetApp()->GetEventManager()->UnSubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
}

void CQBeePointView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_POINT_QBEE;
	m_pState->m_nIdParameter = 0;
}

bool CQBeePointView::IsViewState(State state){
	return state.m_nPosTab == JournalEnum::QUEEN_BEE_JOURNAL &&
		state.m_type == JournalEnum::NONE &&
		state.m_nTypeSel == JournalEnum::NOTSEL;
}

void CQBeePointView::Init(){
	m_infoDrawer.Clear();
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CPointsTable point;
	IResult* pResult = point.GetValue(pDb);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CPointsTable::content* pContent = (CPointsTable::content*)(*pResult)[i];
		CQBInfoPointOperation operation;
		operation.m_content.s_nIdPoint = pContent->s_nId;
		IResult* pResult1 = operation.GetValue(pDb);
		int nNonQB = 0, nExistQB = 0;
		for(int j = 0; j < (int)pResult1->GetSizeResult(); j++){
			CQBInfoPointOperation::content* pContent1 = (CQBInfoPointOperation::content*)(*pResult1)[j];
			if(!pContent1->s_bDead && pContent1->s_nCount)
				nExistQB++;
			else if(!pContent1->s_nCount)
				nNonQB++;
		}
		pResult1->Free();
		int j = m_infoDrawer.AddItem(pContent->s_nId);
		m_infoDrawer.AddSubItem(j, InfoItem(pContent->s_strName, _T("Arial"), 20, true, true));
		std::tstring strTemp(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_POINT_NON_QB_ID) + _T(" - "));
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(nNonQB), _T("Arial"), 14, false, false));
		strTemp = IApplication::GetApp()->GetLanguageManager()->GetString(INFO_POINT_ALL_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(nExistQB), _T("Arial"), 14, false, false));
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
}

void CQBeePointView::EventHandler(){
	Init();
	if(IsCreate())
		Invalidate(FALSE);
}